﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace Xinkui.Spider.Spider
{
    public class HttpUtil
    {
        public static string PostPage(string requestUrl, Dictionary<string, string> postParams)
        {
            HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
            return PostPage(request, postParams, Encoding.UTF8);
        }

        public static string PostPage(string requestUrl, Dictionary<string, string> postParams, Encoding pageEncoding)
        {
            HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
            return PostPage(request, postParams, pageEncoding);
        }

        public static string PostPage(HttpWebRequest request, Dictionary<string, string> postParams)
        {
            return PostPage(request, postParams, Encoding.UTF8);
        }


        private static string ConvertGBK(string input)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(input);
            return Encoding.GetEncoding("GBK").GetString(buffer);
        }

        public static string PostPage(HttpWebRequest request, Dictionary<string, string> postParams, Encoding pageEncoding)
        {
            System.Threading.Thread.Sleep(1000);
            
            string srcString = "";

            // 要提交的字符串数据。格式形如:user=uesr1&password=123
            string postString = "";
            if (postParams != null)
            {
                foreach (string key in postParams.Keys)
                {
                    postString += key + "=" + postParams[key] + "&";
                }
                postString = postString.Substring(0, postString.Length - 1);

            }
            // 将提交的字符串数据转换成字节数组
            byte[] postData = pageEncoding.GetBytes(postString);

            // 设置提交的相关参数
            request.Method = "GET";
            request.KeepAlive = true;
            request.ContentType = "text/html,application/xhtml+xml";
            request.ContentLength = postData.Length;
            request.UserAgent = "Mozilla/5.0 (Windows NT 5.2; rv:34.0) Gecko/20100101 Firefox/34.0";
            //request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.2; rv:34.0) Gecko/20100101 Firefox/34.0");

            // 提交请求数据
            //System.IO.Stream outputStream = request.GetRequestStream();

            //outputStream.Write(postData, 0, postData.Length);
            //outputStream.Close();

            // 接收返回的页面
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            System.IO.Stream responseStream = response.GetResponseStream();
            System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, pageEncoding);
            srcString = reader.ReadToEnd();

            return srcString;
        }

        public static string PostPage(string url, string postContext, Encoding pageEncoding)
        {
            try
            {

                byte[] data = pageEncoding.GetBytes(postContext);
                //   准备请求...  
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "Post";
                req.ContentType = "application/x-www-form-urlencoded";
                req.ContentLength = data.Length;
                Stream stream = req.GetRequestStream();


                //   发送数据  
                stream.Write(data, 0, data.Length);
                stream.Close();


                HttpWebResponse rep = (HttpWebResponse)req.GetResponse();
                Stream receiveStream = rep.GetResponseStream();
                Encoding encode = pageEncoding;
                //   Pipes   the   stream   to   a   higher   level   stream   reader   with   the   required   encoding   format.    
                StreamReader readStream = new StreamReader(receiveStream, encode);

                Char[] read = new Char[256];
                int count = readStream.Read(read, 0, 256);
                StringBuilder sb = new StringBuilder("");
                while (count > 0)
                {
                    String readstr = new String(read, 0, count);
                    sb.Append(readstr);
                    count = readStream.Read(read, 0, 256);
                }

                rep.Close();
                readStream.Close();

                return sb.ToString();

            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
